﻿-- =============================================
-- Author:		GSJ
-- Create date: 2012-6-30
-- Description:	添加往来收入
-- =============================================
CREATE PROCEDURE [dbo].[proc_L_Dealings_Create]
	(
		@O_CompanyId INT,
		@O_CompanyName  VARCHAR(200),
		@O_PaymentTypeId INT,
		@O_PaymentTypeName VARCHAR(200),
		@O_BankId  int,
		@O_BankName VARCHAR(100),
		@I_CompanyId int,
		@I_CompanyName  VARCHAR(200),
		@I_PaymentTypeId int, 
		@I_PaymentTypeName VARCHAR(200),
		@I_BankId int,
		@I_BankName VARCHAR(100),
		@je decimal(18,2),
		@Agent varchar(10),
		@AgentName nVARCHAR(20),
		@PZPerson varchar(10),
		@PZPersonName nVARCHAR(20),
		@SHPerson varchar(10),
		@SHPersonName nVARCHAR(20),
		@UserId varchar(10),
		@UserName nVARCHAR(20),
		@Memo nvarchar(2000),
		@CateOneId int,
		@CateOneName nvarchar(50),
		@CateTwoId int,
		@CateTwoName nvarchar(50),
		@SubjectId int,
		@SubjectName nvarchar(50)
	)
AS
BEGIN
	
BEGIN TRANSACTION--开始事务
DECLARE @errorSun INT --定义错误计数器
SET @errorSun=0 --没错为0
	
	INSERT INTO [L_Dealings]
           ([O_CompanyId]
           ,[O_CompanyName]
           ,[O_PaymentTypeId]
           ,[O_PaymentTypeName]
           ,[O_BankId]
           ,[O_BankName]
           ,[I_CompanyId]
           ,[I_CompanyName]
           ,[I_PaymentTypeId]
           ,[I_PaymentTypeName]
           ,[I_BankId]
           ,[I_BankName]
           ,[je]
           ,[Agent]
           ,[AgentName]
           ,[PZPerson]
           ,[PZPersonName]
           ,[SHPerson]
           ,[SHPersonName]
           ,[UserId]
           ,[UserName]
           ,[Memo],
           CateOneId,
		CateOneName,
		CateTwoId,
		CateTwoName,
		SubjectId,
		SubjectName
         )
     VALUES
           (@O_CompanyId
           ,@O_CompanyName
           ,@O_PaymentTypeId
           ,@O_PaymentTypeName
           ,@O_BankId
           ,@O_BankName
           ,@I_CompanyId
           ,@I_CompanyName
           ,@I_PaymentTypeId
           ,@I_PaymentTypeName
           ,@I_BankId
           ,@I_BankName
           ,@je
           ,@Agent
           ,@AgentName
           ,@PZPerson
           ,@PZPersonName
           ,@SHPerson
           ,@SHPersonName
           ,@UserId
           ,@UserName
           ,@Memo,
            @CateOneId,
			@CateOneName,
			@CateTwoId,
			@CateTwoName,
			@SubjectId,
			@SubjectName
		 )
           
           
if @@ROWCOUNT<1
	begin
		set @errorSun=@errorSun+1		
		goto e;
	end           
	
	DECLARE @pid INT 
--刚插入的报销记录的ID
SET @pid= @@identity
           
Insert Into Finance_Outlay
	(
		CreateDate,
		TradeDate,
		Summary,
		CateOneId,
		CateOneName,
		CateTwoId,
		CateTwoName,
		SubjectId,
		SubjectName,
		PaymentTypeId,
		PaymentTypeName,
		BankId,
		BankName,
		OutlayReason,
		OutlayPrice,
		Agent,
		AgentName,
		PZPerson,
		PZPersonName,
		SHPerson,
		SHPersonName,
		UserName,
		EName,
		Description,
		BillNumber,
		CompanyId,
		CompanyIdCur,
		DepartmentIdCur,l_id,l_type
	)
	VALUES(
		GETDATE(),
		Convert(varchar(10),GETDATE(),120),
		@I_CompanyName,
		@CateOneId,
		@CateOneName,
		@CateTwoId,
		@CateTwoName,
		@SubjectId,
		@SubjectName,
		@O_PaymentTypeId,
		@O_PaymentTypeName,
		@O_BankId,
		@O_BankName,
		@I_CompanyName+'借款',
		@je,
		@Agent,
		@AgentName,
		@PZPerson,
		@PZPersonName,
		@SHPerson,
		@SHPersonName,
		@userid,
		@username,
		'',
		'',
		@O_CompanyId,
		@O_CompanyId,
		0,@pid,'LD'
	)
	
if @@ROWCOUNT<1
	begin
		set @errorSun=@errorSun+1		
		goto e;
    end           
       
       
       
 INSERT INTO [Finance_RunningAccount]
           (
           [CreateDate]
           ,[TradeDate]
           ,[Summary]
           ,[AccountsTypeId]
           ,[AccountsTypeName]
           ,[ParentId]
           ,[ParentName]
           ,[SubjectId]
           ,[SubjectName]
           ,[PaymentTypeId]
           ,[PaymentTypeName]
           ,[BankId]
           ,[BankName]
           ,[Income]
           ,[Outlay]
           ,[Agent]
           ,[AgentName]
           ,[UserName]
           ,[EName]
           ,[CompanyId]
           ,[Description]
           ,[BillNumber]
           ,[OrderId]
           ,[Renling]
           ,[IOTypeId]
           ,[isGreen],l_id)
   
         values ( 
           GETDATE()
           ,CONVERT(VARCHAR(10),GETDATE(),120)
           ,@I_CompanyName
           ,1
           ,'收入'
           ,0
           ,''
           ,0
           ,''
           ,@I_PaymentTypeId
           ,@I_PaymentTypeName
           ,@I_BankId
           ,@I_BankName
           ,@je
           ,0
           ,@Agent
           ,@AgentName
           ,@UserId
           ,@UserName
           ,@I_CompanyId
           ,@Memo
           ,''
           ,''
           ,2
           ,0
           ,0,@pid)

           
if @@ROWCOUNT<1
	begin
		set @errorSun=@errorSun+1		
		goto e;
    end      
       
       
       
           
e:
    PRINT @errorSun
	if @errorSun>0
	begin
		rollback TRANSACTION
	
	end
	else
	begin
		commit TRANSACTION
	END 
 
 
 
 
           
           
END
